const app = getApp()
const passwordReg = /^[A-Za-z0-9\.@_]{6,24}$/
import { requestApi } from '../../../api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    originalPwd: '',
    newPwd: '',
    surePwd: '',
    channel: '',
  },
  // save
  formSubmit: function (e) {
    let originalPwd = e.detail.value.originalPwd
    let newPwd = e.detail.value.newPwd
    let surePwd = e.detail.value.surePwd
    let tip = ''

    if (!passwordReg.test(originalPwd) || !passwordReg.test(newPwd) || !passwordReg.test(surePwd)){
      tip = '密码应由6-24位的数字、字母组成'
    } else if (newPwd != surePwd ) {
      tip = '新密码和确认密码不一致'
    }
    if (tip) {
      wx.showModal({
        title: '提示',
        content: tip,
        showCancel: false
      })
      return
    }
   let params = {
     old_password: originalPwd,
     password: newPwd,
     channel: this.data.channel
    }

    // 请求接口
    app.request({
      url: requestApi.setPasswordUrl,
      data: params,
      method: 'POST'
    },(data)=> {
      wx.showToast({
        title: data.original.msg,
        duration: 1000,
        success: (res)=> {
          wx.navigateBack({})
        }
      }) 
    }, (error) => {
        wx.showModal({
          title: '提示',
          content: error,
          showCancel: false
        })
      })
  },
   
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'

    this.setData({
      channel: channel
    })
  },
})